import router from '@/router'
import Nprogress from 'nprogress'
import getPageTitle from '@/utils/get-page-title'
import { getUserInfo } from '@/utils/auth'
import { Message } from 'element-ui'

router.beforeEach((to, from, next) => {
  Nprogress.start()
  const userInfo = getUserInfo()
  if (userInfo) {
    if (to.path === '/login') {
      next(false)
      Nprogress.done()
      Message.warning('您已经登录')
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
      Nprogress.done()
      Message.warning('请您登录')
    }
  }
})

router.afterEach((to, from) => {
  Nprogress.done()
  document.title = getPageTitle(to.meta.title)
})
